#       config_dialog.py
#       
#       Copyright 2010 Alexey Zotov <alexey.zotov@gmail.com>
#       
#       This program is free software; you can redistribute it and/or modify
#       it under the terms of the GNU General Public License as published by
#       the Free Software Foundation; either version 2 of the License, or
#       (at your option) any later version.
#       
#       This program is distributed in the hope that it will be useful,
#       but WITHOUT ANY WARRANTY; without even the implied warranty of
#       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#       GNU General Public License for more details.
#       
#       You should have received a copy of the GNU General Public License
#       along with this program; if not, write to the Free Software
#       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#       MA 02110-1301, USA.

# -*- coding: utf8 -*-

from PyQt4.QtCore import Qt
from PyQt4 import QtGui
from PyKDE4.kdecore import i18n
from PyKDE4.kdeui import KIcon, KPageDialog

import logger

from config_auth import ConfigAuth
from config_requests import ConfigRequests
from config_scheduler import ConfigScheduler
from config_icon import ConfigIcon
from config_results import ConfigResults
from config_duplicates import ConfigDuplicates

class ConfigDialog(KPageDialog):
    def __init__(self, settings, duplicates=False, parent=None,
        flags=Qt.WindowFlags()):
        KPageDialog.__init__(self, parent, flags)
        self.duplicates = duplicates
        self.setWindowTitle(i18n('New Wallpaper configuration'))
        self.setFaceType(KPageDialog.List)
        self.setButtons(KPageDialog.ButtonCode(
            KPageDialog.Ok | KPageDialog.Cancel))
        
        self.config_auth = ConfigAuth(settings, self)
        self.config_auth_page = self.addPage(
            self.config_auth, i18n('Account'))
        
        self.config_requests = ConfigRequests(settings, self)
        self.config_requests_page = self.addPage(
            self.config_requests, i18n('Requests'))
        
        self.config_scheduler = ConfigScheduler(settings, self)
        self.config_scheduler_page = self.addPage(
            self.config_scheduler, i18n('Schedule'))

        self.config_icon = ConfigIcon(settings, self)
        self.config_icon_page = self.addPage(
            self.config_icon, i18n('Icon'))

        self.config_results = ConfigResults(settings, self)
        self.config_results_page = self.addPage(
            self.config_results, i18n('Results'))

        if duplicates:
            self.config_duplicates = ConfigDuplicates(settings, self)
        else:
            self.config_duplicates = QtGui.QLabel(
                i18n('To search for duplicates' \
                    ' you need python with sqlite support' \
                    ' and python-imaging installed')
            )
            self.config_duplicates.setAlignment(
                Qt.AlignHCenter | Qt.AlignVCenter)
            self.config_duplicates.setWordWrap(True)
        self.config_duplicates_page = self.addPage(
            self.config_duplicates, i18n('Duplicates'))

        self.config_auth.auth_changed.connect(
            self.config_requests.auth_changed)
        self.config_auth.auth_changed.connect(
            self.config_scheduler.auth_changed)
        self.config_auth.auth_changed.connect(
            self.config_icon.auth_changed)

    @logger.log_exc
    def accept(self):
        if not self.config_requests.is_valid():
            self.setCurrentPage(self.config_requests_page)
            return

        if not self.config_scheduler.is_valid():
            self.setCurrentPage(self.config_scheduler_page)
            return

        KPageDialog.accept(self)

    def export_settings(self):
        settings = {}
        settings.update(self.config_auth.export_settings())
        settings.update(self.config_requests.export_settings())
        settings.update(self.config_scheduler.export_settings())
        settings.update(self.config_icon.export_settings())
        settings.update(self.config_results.export_settings())
        if self.duplicates:
            settings.update(self.config_duplicates.export_settings())
        return settings
